package com.buaa.divinationmaster.core.algorithm;

/**
 * 占卜算法异常类
 * 用于处理占卜过程中出现的各种异常情况
 */
public class DivinationException extends Exception {
    
    public enum ErrorType {
        INVALID_INPUT("输入参数无效"),
        ALGORITHM_ERROR("算法执行错误"),
        DATA_NOT_FOUND("数据未找到"),
        NETWORK_ERROR("网络错误"),
        STORAGE_ERROR("存储错误"),
        UNKNOWN_ERROR("未知错误");
        
        private final String description;
        
        ErrorType(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    private ErrorType errorType;
    private String errorCode;
    
    public DivinationException(String message) {
        super(message);
        this.errorType = ErrorType.UNKNOWN_ERROR;
    }
    
    public DivinationException(String message, Throwable cause) {
        super(message, cause);
        this.errorType = ErrorType.UNKNOWN_ERROR;
    }
    
    public DivinationException(ErrorType errorType, String message) {
        super(message);
        this.errorType = errorType;
    }
    
    public DivinationException(ErrorType errorType, String message, Throwable cause) {
        super(message, cause);
        this.errorType = errorType;
    }
    
    public DivinationException(ErrorType errorType, String errorCode, String message) {
        super(message);
        this.errorType = errorType;
        this.errorCode = errorCode;
    }
    
    public ErrorType getErrorType() {
        return errorType;
    }
    
    public String getErrorCode() {
        return errorCode;
    }
    
    public String getLocalizedMessage() {
        if (errorType != null) {
            return errorType.getDescription() + ": " + getMessage();
        }
        return getMessage();
    }
    
    @Override
    public String toString() {
        return "DivinationException{" +
                "errorType=" + errorType +
                ", errorCode='" + errorCode + '\'' +
                ", message='" + getMessage() + '\'' +
                '}';
    }
}
